//
//  SERegistrationConfirmController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/7.
//

import UIKit

enum SERegistrationDetailControllerType {
    case confirm
    case detail(status: Int)

    var confirmBtnTitle: String {
        switch self {
        case .confirm: return "返回首页"
        case .detail(let status):
            return status == 0 ? "再次预约" : "取消预约"
        }
    }

    var confirmBtnTitleColor: UIColor {
        switch self {
        case .confirm: return Asset.white.color
        case .detail: return Asset.main.color
        }
    }

    var confirmBtnbgColor: UIColor {
        switch self {
        case .confirm: return Asset.main.color
        case .detail:  return Asset.white.color
        }
    }
}

class SERegistrationDetailController: SEBaseViewController {

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.isHidden = true
            tableView.tableHeaderView = tableHeaderView
            tableView.tableFooterView = tableFooterView
            tableView.register(UINib(nibName: SERegistrationDetailCell.className, bundle: nil), forCellReuseIdentifier: SERegistrationDetailCell.reuseId)
        }
    }

    lazy var tableHeaderView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: 15))
        view.backgroundColor = UIColor.clear
        return view
    }()

    lazy var tableFooterView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: 48))
        let btnView = SERegistrationConfirmBtnView(frame: CGRect(x: 15, y: 0, width: UIScreen.screenWidth - 30, height: 48))
        btnView.delegate = self
        btnView.title = type.confirmBtnTitle
        btnView.titleColor = type.confirmBtnTitleColor
        btnView.btnBgColor = type.confirmBtnbgColor
        view.addSubview(btnView)
        return view
    }()

    var viewModel = SEMyRegistrationViewModel(withLoading: true)
    let sourceId: String
    let type: SERegistrationDetailControllerType
    var canceledHandler: (() -> Void)?

    init(with sourceId: String, type: SERegistrationDetailControllerType) {
        self.sourceId = sourceId
        self.type = type
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "预约详情"
        requestRegistrationDetail()
    }

    override func setupUI() {
        super.setupUI()
        
        viewModel.updateData = {status in
            switch status {
            case .success:
                self.tableView.isHidden = false
                self.tableView.reloadData()
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requestRegistrationDetail)) {
                    SEHUD.show(message: msg)
                }
            case .failure(let msg):
                SEHUD.show(message: msg)
            }
        }

        viewModel.cancelHandler = {status in
            switch status {
            case .success:
                SEHUD.show(message: "取消成功")
                self.canceledHandler?()
                self.navigationController?.popViewController(animated: true)
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requestCancelRegistration)) {
                    SEHUD.show(message: msg)
                }
            case .failure(let msg):
                SEHUD.show(message: msg)
            }
        }
    }
}

// MARK: - UITableViewDataSource,UITableViewDelegate

extension SERegistrationDetailController: UITableViewDataSource, UITableViewDelegate {

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SERegistrationDetailCell.reuseId, for: indexPath) as! SERegistrationDetailCell
        cell.updateCell(model: viewModel.registrationModel(indexPath: indexPath))
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.numberOfSections
    }

    // MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 253
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 42
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        return view
    }

}

// MARK: - SERegistrationConfirmBtnViewDelegate

extension SERegistrationDetailController: SERegistrationConfirmBtnViewDelegate {
    func registrationConfirmBtnViewAction(view: SERegistrationConfirmBtnView) {
        switch type {
        case .confirm:
            navigationController?.popToRootViewController(animated: true)
        case .detail(let status):
            if status == 1 {
                guard viewModel.registrationList.count == 1 else {
                    return
                }
                let model = viewModel.registrationModel(indexPath: IndexPath(row: 0, section: 0))
                viewModel.selectedModel = model
                requestCancelRegistration()
            } else {
                let vc = SEHospitalListViewController(with: .registration)
                navigationController?.pushViewController(vc, animated: true)
            }

        }
    }
}

// MARK: - APIRequest

extension SERegistrationDetailController {
    @objc private func requestRegistrationDetail() {
        viewModel.requestMyRegistrationList(id: sourceId)
    }

    @objc private func requestCancelRegistration() {
        viewModel.requestCancelRegistration()
    }
}
